/**
 * 
 */
package controller.xml;

import model.solver.ImportSolverParameter;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link ImportSolverParameter} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class ImportedSolverParemeterXMLHandler extends XMLHandler {
	private ImportSolverParameter				param	= null;
	private ImportedSolverParameterXMLStatus	status	= ImportedSolverParameterXMLStatus.NONE;

	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
		case SOLVER_PARAMETER:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case SOLVER_PARAMETER:
			if (qName.equals(XMLBaliseFormat.SOLVER_PARAMETER))
			{// ferme solver param
				status = ImportedSolverParameterXMLStatus.NONE;
				param = new ImportSolverParameter(((SolverParameterXMLHandler) delegate).getParam());
				delegate = null;
				return true;
			}
			throw new SAXException("end of SOLVER PARAMETER was expected");

		case CNF_POS:
			if (qName.equals(XMLBaliseFormat.IMPORT_CNF_POSITION))
			{// ferme cnf pos
				setCNFPos();
				status = ImportedSolverParameterXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of CNF_POSITION was expected");
		case ARG:
			if (qName.equals(XMLBaliseFormat.IMPORT_ARG))
			{// ferme arg
				addArg();
				status = ImportedSolverParameterXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of ARG was expected");

		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}


	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != ImportedSolverParameterXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.SOLVER_PARAMETER))
		{// balise parameter
			if (param != null)
				throw new SAXException("already have a SOLVER PARAMETER");

			// set les donnees pour recupere le basic result
			status = ImportedSolverParameterXMLStatus.SOLVER_PARAMETER;
			delegate = new SolverParameterXMLHandler();

			return true;
		}

		// si pas de param refuse les autres
		if (param == null)
			throw new SAXException("need a SOLVER PARAMETER	 first");

		if (arg2.equals(XMLBaliseFormat.IMPORT_CNF_POSITION))
		{// balise cnf position
			status = ImportedSolverParameterXMLStatus.CNF_POS;
			return true;
		}

		if (arg2.equals(XMLBaliseFormat.IMPORT_ARG))
		{// balise arg
			status = ImportedSolverParameterXMLStatus.ARG;
			return true;
		}
		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the param.
	 * 
	 * @return the param
	 */
	public ImportSolverParameter getParam() {
		return param;
	}

	/**
	 * Set the cnf position the {@link ImportSolverParameter}.
	 * 
	 * @throws SAXException
	 */
	private void setCNFPos() throws SAXException {
		Integer val = null;
		try
		{
			val = Integer.valueOf(buffer.toString());
		} catch (Exception e)
		{
			throw new SAXException("bad integer value for CNF POSITION");
		}
		if (!param.setCnfposition(val))
			throw new SAXException("bad CNF position");
	}


	private void addArg() {
		param.addParameter(buffer.toString());
	}

}
